﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Windows;
using System.Windows.Controls;
using Vtron.LEDCorrective.Core;
using Vtron.LEDCorrective.Domain.LEDSender;

namespace Vtron.LEDCorrective.Pages
{
    /// <summary>
    /// Partition.xaml 的交互逻辑
    /// </summary>
    public partial class Partition : UserControl
    {
        public Partition()
        {
            InitializeComponent();
            Loaded += Partition_Loaded;
        }
        CorrectiveEngine correctiveEngine = CorrectiveEngine.Instance;
        private void Partition_Loaded(object sender, RoutedEventArgs e)
        {
            if (correctiveEngine.PartitionColumn == 0 || correctiveEngine.PartitionRow == 0)
            {
                column_txt.Text = 192.ToString();
                row_txt.Text = 192.ToString();
            }
            else
            {
                column_txt.Text = correctiveEngine.PartitionColumn.ToString();
                row_txt.Text = correctiveEngine.PartitionRow.ToString();
            }

            if (correctiveEngine.BlockColumn == 1)
            {
                rb1.IsChecked = true;
            }
            else if (correctiveEngine.BlockColumn == 2)
            {
                rb2.IsChecked = true;
            }
            else if (correctiveEngine.BlockColumn == 3)
            {
                rb3.IsChecked = true;
            }
            else if (correctiveEngine.BlockColumn == 4)
            {
                rb4.IsChecked = true;
            }
        }

        private void partition_btn_Click(object sender, RoutedEventArgs e)
        {
            int column = 0;
            int row = 0;
            if (!Int32.TryParse(column_txt.Text, out column))
            {
                ModernDialog.ShowMessage("单分区内像素列数必须为正整数，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }
            else if (column == 0)
            {
                ModernDialog.ShowMessage("单分区内像素列数必须大于0，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }
            if (!Int32.TryParse(row_txt.Text, out row))
            {
                ModernDialog.ShowMessage("单分区内像素行数必须为正整数，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }
            else if (row == 0)
            {
                ModernDialog.ShowMessage("单分区内像素行数必须大于0，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }

            if ((bool)rb1.IsChecked)
            {
                if (!(column % 1 == 0 && row % 1 == 0))
                {
                    ModernDialog.ShowMessage("单分区内像素行数及列数必须是分块拍摄行列数目的整数倍，请重新输入！", "提示", MessageBoxButton.OK);
                    return;
                }
            }
            else if ((bool)rb2.IsChecked)
            {
                if (!(column % 2 == 0 && row % 2 == 0))
                {
                    ModernDialog.ShowMessage("单分区内像素行数及列数必须是分块拍摄行列数目的整数倍，请重新输入！", "提示", MessageBoxButton.OK);
                    return;
                }
            }
            else if ((bool)rb3.IsChecked)
            {
                if (!(column % 3 == 0 && row % 3 == 0))
                {
                    ModernDialog.ShowMessage("单分区内像素行数及列数必须是分块拍摄行列数目的整数倍，请重新输入！", "提示", MessageBoxButton.OK);
                    return;
                }
            }
            else if ((bool)rb4.IsChecked)
            {
                if (!(column % 4 == 0 && row % 4 == 0))
                {
                    ModernDialog.ShowMessage("单分区内像素行数及列数必须是分块拍摄行列数目的整数倍，请重新输入！", "提示", MessageBoxButton.OK);
                    return;
                }
            }

            SCP_WALLS_T wall = correctiveEngine.GetWall();

            if (wall.wallNum == 1)
            {
                correctiveEngine.ResolWidth = (int)wall.wall[0].resol.width;
                correctiveEngine.ResolHeight = (int)wall.wall[0].resol.height;
                correctiveEngine.ScaleColumn = (int)wall.wall[0].scale.col;
                correctiveEngine.ScaleRow = (int)wall.wall[0].scale.row;
                correctiveEngine.PartitionColumn = column;
                correctiveEngine.PartitionRow = row;
                if ((bool)rb1.IsChecked)
                {
                    correctiveEngine.BlockColumn = 1;
                    correctiveEngine.BlockRow = 1;
                }
                else if ((bool)rb2.IsChecked)
                {
                    correctiveEngine.BlockColumn = 2;
                    correctiveEngine.BlockRow = 2;
                }
                else if ((bool)rb3.IsChecked)
                {
                    correctiveEngine.BlockColumn = 3;
                    correctiveEngine.BlockRow = 3;
                }
                else if ((bool)rb4.IsChecked)
                {
                    correctiveEngine.BlockColumn = 4;
                    correctiveEngine.BlockRow = 4;
                }
                correctiveEngine.Scp_wall = wall.wall[0];
                topological_btn.IsEnabled = true;

                correctiveEngine.LedCollections.Clear();
                for (int i = 0; i < correctiveEngine.PartitionCount; i++)
                {
                    LEDCollection ledCollection = new LEDCollection(column, row);
                    correctiveEngine.LedCollections.Add(ledCollection);
                }

                ModernDialog.ShowMessage("生成分区成功！", "信息", MessageBoxButton.OK);
            }
            else if (wall.wallNum == 0)
            {
                ModernDialog.ShowMessage("未分配默认墙体，请分配后再校正！", "提示", MessageBoxButton.OK);
                return;
            }
            else if (wall.wallNum > 1)
            {
                ModernDialog.ShowMessage("系统错误，默认墙体大于一个！", "错误", MessageBoxButton.OK);
                return;
            }
        }

        private void topological_btn_Click(object sender, RoutedEventArgs e)
        {
            TopologicalWindow window = new TopologicalWindow();
            window.Show();
        }
    }
}
